package cn.hd.controller;


import cn.hd.entity.Advertise;
import cn.hd.entity.Result;
import cn.hd.service.AdvertiseService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
public class AdvertiseController {
    @Autowired
    private AdvertiseService advertiseService;

    @RequestMapping("/selectAdvertise")
    public Object selectAdvertise(int pageNum,int pageSize,String adname){
        Map map = new HashMap();
        map.put("adname",adname);
        PageInfo<Advertise> page = advertiseService.selectAdvertise(pageNum, pageSize, map);
        if(page.getList().size()!=0){
            return new Result("200","查询成功！",page);
        }else {
            return new Result("400","查询失败！",null);
        }
    }

    //多条件分页动态查询
    @RequestMapping("/selectAllFenYeAdvertise")
    public Object selectAllFenYeAdvertise(int pageNum,int pageSize){
        PageInfo<Advertise> page = advertiseService.selectAllFenYeAdvertise(pageNum, pageSize);
        if(page.getList().size()!=0){
            return new Result("200","查询成功！",page);
        }else {
            return new Result("400","查询失败！",null);
        }
    }

    //图片预览
    @RequestMapping("/showImg")
    public void showImg(String filename, HttpServletResponse response) throws Exception{
        File file=new File("D:\\imgs",filename);
        FileInputStream fin=new FileInputStream(file);
        //预览
        response.setHeader("Content-Disposition", "filename="+filename);
        ServletOutputStream fout = response.getOutputStream();
        IOUtils.copy(fin,fout);
        fout.close();
        fin.close();
    }

    //上传图片  单独上传图片，把上传后的图片名称返回给前端
    @RequestMapping("/uploads")
    public  Object Uploads(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        //判断是否有/
        if (filename.contains("/")){
            filename=filename.substring(filename.lastIndexOf("/"+1));
        }
        //唯一处理
        filename=UUID.randomUUID().toString()+"-"+filename;
        //上传
        file.transferTo(new File("d:/imgs",filename));

        return new Result("200",filename,null);
    }

    //添加
    @RequestMapping("/addAdvertise")
    public Result addAdvertise(Advertise advertise){
        int i = advertiseService.addAdvertise(advertise);
        if(i!=0){
            return new Result("200","添加成功！",null);
        }else{
            return new Result("400","添加失败！",null);
        }
    }


    //根据编号查询
    @RequestMapping("/selectAdvertiseById")
    public Object selectAdvertiseById(long adid){
        Advertise advertise = advertiseService.selectAdvertiseById(adid);
        if (advertise!=null){
            return new Result("200","查询成功！",advertise);
        }else{
            return new Result("400","查询失败！",null);
        }

    }

    //修改
    @RequestMapping("/updateAdvertise")
    public Object updateAdvertise(Advertise advertise){
        int i = advertiseService.updateAdvertise(advertise);
        if(i!=0){
            return new Result("200","修改成功！",null);
        }else{
            return new Result("400","修改失败！",null);
        }
    }

    //删除
    @RequestMapping("/deleteAdvertise")
    public Object deleteAdvertise(long adid){
        int i = advertiseService.deleteAdvertise(adid);
        if(i!=0){
            return new Result("200","删除成功！",null);
        }else{
            return new Result("400","删除失败！",null);
        }
    }

    //修改审核状态为成功
    @RequestMapping("/updateAdvertisePass")
    public Object updateAdvertisePass(long adid){
        int i = advertiseService.updateAdvertisePass(adid);
        if(i!=0){
            return new Result("200","修改成功！",null);
        }else{
            return new Result("400","修改失败！",null);
        }
    }

    //修改审核状态为失败
    @RequestMapping("/updateAdvertiseFail")
    public Object updateAdvertiseFail(long adid){
        int i = advertiseService.updateAdvertiseFail(adid);
        if(i!=0){
            return new Result("200","修改成功！",null);
        }else{
            return new Result("400","修改失败！",null);
        }
    };
}
